<?php
namespace app\avikcm\controller;

use app\avikcm\model\goodsCategory as ModelGoodsCategory;
use clt\Leftnav;
use think\Db;
use think\facade\Request;
use think\facade\Env;
class GoodsCategory extends Common
{
    function initialize()
    {
        parent::initialize();
    }
    public function index(){
        if(request()->isPost()){
            $arr = Db::name('goods_category')->where('del',0)->order('sort desc')->select();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$arr,'is'=>true,'tip'=>'操作成功'];
        }
        return view();
    }
    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            $data['create_time'] = date('Y-m-d H:i:s');
            if($data['pid'] == 0){
                return $result = ['code' => 0, 'msg' => '请选择上级分类!'];
            }
            db('goods_category')->insert($data);
            return $result = ['code'=>1,'msg'=>'添加成功!','url'=>url('index')];
        }
        $nav = new Leftnav();
        $list = ModelGoodsCategory::all(function($query){
            $query->where('del', 0)->order('sort', 'desc');
        });
        $arr = $nav->menu($list);
        $this->assign('list',$arr);
        $this->assign('title','添加分类');
        return $this->fetch('form');
    }
    public function order(){
        $db=db('goods_category');
        $data = input('post.');
        if($db->update($data)!==false){
            return $result = ['code'=>1,'msg'=>'排序更新成功!','url'=>url('index')];
        }else{
            return $result = ['code'=>0,'msg'=>'排序更新失败!'];
        }
    }

    public function del(){
        $id = input('post.id');
        $exists = Db::name('goods_category')->where('pid', $id)->where('del', 0)->find();
        if($exists){
            return ['code' => 0, 'msg' => '此分类下有正在使用的子分类，无法删除!'];
        }
        else{
            $this->delauth($id);
            return $result = ['code'=>1,'msg'=>'删除成功!'];
        }
    }
    public function delauth($id){
        db('goods_category')->where('id',$id)->update(['del'=>1]);
        $authSon = Db::name('goods_category')->where('pid',$id)->select();
        if(!empty($authSon)){
            foreach ($authSon as $k=>$v){
                db('goods_category')->where('id',$v['id'])->update(['del'=>1]);
            }
        }
        return true;
    }

    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
            $res = db('goods_category')->where('id',$data['id'])->update($data);
            if($res) {
                return json(['code' => 1, 'msg' => '保存成功!', 'url' => url('index')]);
            } else {
                return json(['code' => 0, 'msg' =>'保存失败！']);
            }
        }else{
            $id = input('id');
            $info = db('goods_category')->where('id',$id)->find();
            $nav = new Leftnav();
            $list = ModelGoodsCategory::all(function($query){
                $query->order('sort', 'desc');
            });
            $arr = $nav->menu($list);
            $this->assign('list',$arr);
            $this->assign('info',$info);
            $this->assign('title','编辑分类');
            return $this->fetch('form');
        }
    }
    //设置展示
    public function setOpen()
    {
        $id = input('post.id');
        $is_show = input('post.is_show');
        if (db('goods_category')->where('id=' . $id)->update(['is_show' => $is_show]) !== false) {
            return ['status' => 1, 'msg' => '设置成功!'];
        } else {
            return ['status' => 0, 'msg' => '设置失败!'];
        }
    }
}